package xupt.com.bolg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xupt.com.bolg.entity.*;
import xupt.com.bolg.repository.BolgRepository;
import xupt.com.bolg.service.impl.BolgServiceImpl;
import xupt.com.bolg.service.impl.ComnServiceImpl;
import xupt.com.bolg.service.impl.DotServiceImpl;
import xupt.com.bolg.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class BolgController {
    @Autowired
    BolgServiceImpl bolgService;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    ComnServiceImpl comnService;
    @Autowired
    DotServiceImpl dotService;
    @Autowired
    BolgRepository bolgRepository;

    @PostMapping("/sendBlog")
    public String sendBolg(Bolg bolg, HttpServletRequest request, HttpServletResponse response) {
        String name= (String) request.getSession().getAttribute("name");
        bolg.setUserName(name);
        User byName = userService.findByName(name);
        bolg.setUserid(byName.getId());
        bolgService.save(bolg);
        return "homepage";
    }
    @GetMapping("/sendBolg1")
    public String skipbolg(){
        return "bolg/bolg";
    }

    //查看博客
    //查看博客信息的时候把Bolg 的id 传进去，评论点赞的时候删除
    @GetMapping("/bolg")
    public String bolgInformation(HttpServletRequest request,Model model){
        String s= request.getParameter("id");
        Integer integer = Integer.valueOf(s);
        int id = integer.intValue();
        request.getSession().setAttribute("bolgid",id);
        //把评论塞进去了
        List<Comn> comns = comnService.findcomnByBolgID(id);
        model.addAttribute("comns",comns);
        Bolg byID = bolgService.findByID(id);
        model.addAttribute("bolg",byID);
        return "bolg/bolginformation";
    }
    @GetMapping("/user/bolg/delete")
    public String deleteBolg(HttpServletRequest request,Model model){
        String name =(String) request.getSession().getAttribute("name");
        String id = request.getParameter("id");
        Integer integer = Integer.valueOf(id);
        bolgService.deleteByID(integer);
        List<Bolg> all = bolgService.findAll();
        model.addAttribute("bolgs",all);
        User byName = userService.findByName(name);
        if(byName.getManager()==1){
            return "bolg/managerbolglist";
        }
        return "bolg/bolglist";
    }
    @PostMapping("/findbolgbyname")
    public String fingbolgByName(HttpServletRequest request,Model model,@RequestParam(value = "name") String name){
        String  name1 =(String) request.getSession().getAttribute("name");
        User byName = userService.findByName(name1);
        List<Bolg>bolglist=new ArrayList<>();
        List<Bolg> all = bolgService.findAll();
        for(int i=0;i<all.size();i++){
            if(all.get(i).getUserName().equals(name)){
                bolglist.add(all.get(i));
            }
        }
        model.addAttribute("bolgs",bolglist);
        if(byName.getManager()==1){
            return "bolg/managerbolglist";
        }
        return "bolg/bolglist";
    }
    @GetMapping("/user/bolg/look")
    public String lookBolg(HttpServletRequest request,Model model){
        //href 动态拼串只能在 request 里面获取
        String id = request.getParameter("id");
        Integer integer = Integer.valueOf(id);
        Bolg byID = bolgService.findByID(integer);
        model.addAttribute("bolg",byID);

        //评论也发送过去了，为什么没有显示？？？,通过bolg_id查一下这个评论有多少内容试试
        List<Comn> comns = comnService.findcomnByBolgID(byID.getId());
        //测试一下数据是否发过去了
        System.out.println("bolg.id"+byID.getId()+"评论内容："+comns.toString());
        model.addAttribute("comns",comns);
        //传入bolg id
        request.getSession().setAttribute("bolgid",byID.getId());
        return "bolg/bolginformation";
    }

    @GetMapping("/bolglow")
    public String lowbolg(HttpServletRequest request,Model model){
        String s= request.getParameter("id");
        Integer bolgid = Integer.valueOf(s);
        String name =(String) request.getSession().getAttribute("name");
        User byName = userService.findByName(name);
        int userid = byName.getId();
        Dot dot = dotService.findDot(bolgid, userid);
        Bolg bolg = bolgService.findByID(bolgid);
        bolg.setLow(bolg.getLow()+1);
        bolgService.updateBolg(bolg);
        if(dot==null){
            Dot dot1=new Dot();
            dot1.setMaycai(1);
            dot1.setMeid(userid);
            dot1.setHeid(bolgid);
            dotService.saveDot(dot1);
        }else if(dot.getMaycai()==0){
            dot.setMaycai(1);
            dotService.saveDot(dot);
        } else{
            bolg.setLow(bolg.getLow()-1);
            bolgService.updateBolg(bolg);
           request.setAttribute("msg","不可以重复踩");
        }
        model.addAttribute("bolg",bolgService.findByID(bolgid));
        model.addAttribute("comns",comnService.findcomnByBolgID(bolgid));
        return "bolg/bolginformation";
    }

    @GetMapping("/bolgup")
    public String upbolg(HttpServletRequest request,Model model){
        String s= request.getParameter("id");
        Integer bolgid = Integer.valueOf(s);
        String name =(String) request.getSession().getAttribute("name");
        User byName = userService.findByName(name);
        int userid = byName.getId();
        Dot dot = dotService.findDot(bolgid, userid);
        Bolg bolg = bolgService.findByID(bolgid);
        bolg.setUp(bolg.getUp()+1);
        bolgService.updateBolg(bolg);
        if(dot==null){
            Dot dot1=new Dot();
            dot1.setMayzan(1);
            dot1.setMeid(userid);
            dot1.setHeid(bolgid);
            dotService.saveDot(dot1);
        }else if(dot.getMayzan()==0){
            dot.setMayzan(1);
            dotService.saveDot(dot);
        } else{
            bolg.setUp(bolg.getUp()-1);
            bolgService.updateBolg(bolg);
            request.setAttribute("msg","不可以重复赞");
        }
        model.addAttribute("bolg",bolgService.findByID(bolgid));
        model.addAttribute("comns",comnService.findcomnByBolgID(bolgid));
        return "bolg/bolginformation";
    }
    @GetMapping("/user/bolg/update")
    public String skipUpdateBolg(Model model,HttpServletRequest request){
        String bolgid = request.getParameter("id");
        Integer id = Integer.valueOf(bolgid);
        Bolg byID = bolgService.findByID(id);
        model.addAttribute("bolg",byID);
        request.getSession().setAttribute("updatebolg",byID.getId());
        return "bolg/updatebolg";
    }
    @PostMapping("/user/bolg/update")
    public String updateBolg(Bolg bolg,HttpServletRequest request,Model model){
        Integer updatebolg = (Integer) request.getSession().getAttribute("updatebolg");
        Integer integer = Integer.valueOf(updatebolg);
        Bolg byID = bolgService.findByID(integer);
        if(bolg.getTitle()!=null){
            byID.setTitle(bolg.getTitle());
        }
        if(bolg.getInformation()!=null){
            byID.setInformation(bolg.getInformation());
        }
        bolgService.save(byID);
        String name =(String) request.getSession().getAttribute("name");
        List<Bolg>bolglist=new ArrayList<>();
        List<Bolg> all = bolgService.findAll();
        for(int i=0;i<all.size();i++){
            if(all.get(i).getUserName().equals(name)){
                bolglist.add(all.get(i));
            }
        }
        model.addAttribute("bolgs",bolglist);
        return "bolg/bolglist";
    }
    //博客分页--------------------
    @GetMapping("/look/bolg/list")  //博客的关键字应该是某人的名字,userName
    public String lookBlog(PageInfor pageInfor,Model model, HttpServletRequest request){
        String name=null;
        int pageNumber=0;
        int size=10;
        if(pageInfor.getSize()!=null&&pageInfor.getSize()!=0){
            size=pageInfor.getSize();
        }
        if(pageInfor.getPageNumber()!=null&&pageInfor.getPageNumber()!=0){
            pageNumber=pageInfor.getPageNumber();
        }
        if(pageInfor.getWord()!=null){
           name=pageInfor.getWord();
        }
        Bolg bolg=new Bolg();
        bolg.setUserName(name);
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withMatcher("userName",ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("id","up","low","information","title","data","userid");
        Example example=Example.of(bolg,matcher);
        Pageable pageable=PageRequest.of(pageNumber,size, Sort.Direction.ASC,"id");
        Page all = bolgRepository.findAll(example, pageable);
        model.addAttribute("bolgs",all.getContent());
        System.out.println("---------收到的pageNumber值是-------------pageNumber:"+pageNumber);
        //给addAttribute写信息
        //发送下一页，和关键字信息，页面大小，总页数
        int number = all.getNumber()+1; //设置下一页
        System.out.println("**********写入下一页Number的值是:**************Number:"+number);
        int totalPages = all.getTotalPages();
        request.setAttribute("pageNumber",number);
        request.setAttribute("word",name);
        request.setAttribute("size",size);
        request.setAttribute("totalPages",totalPages);

        return "bolg/bolglist";
    }
}
